package nocuffin.m5;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


/** 
 * Team: No Cuffin'
 * 2/27/2012
 * 
 *
 * This will be our MainUI!!
 * 
 * 
 * @author Robert Allen, Philip Johnson, Steven Moore
 * @version 1.0 2/27/2012
 *
 */ 
public class MainUI extends Activity{
	Button bBack, bCalendar, bTask;
	TextView label, recenttasks;
	MapView map;
	TaskView task;
	String entries, collected, name;
	String contents[], myList[];
	Bundle b;
	
	@Override
	 public void onCreate(Bundle savedInstanceState) {
		 super.onCreate(savedInstanceState);
	     setContentView(R.layout.main3);
	     bBack = (Button) findViewById(R.id.backButton);
	     bCalendar = (Button) findViewById(R.id.calendarButton);
	     bTask = (Button) findViewById(R.id.taskButton);
	     label = (TextView) findViewById (R.id.emptylabel);
	     recenttasks = (TextView) findViewById (R.id.TextView02);
	     b = getIntent().getExtras(); 
	     name = b.getString("name");
	  
	  
		
		 
	     bCalendar.setOnClickListener(new View.OnClickListener() {
	    	public void onClick(View v) 
	    	{
	    		 Intent myIntent = new Intent(v.getContext(), Map.class);   
		    	 Bundle b = getIntent().getExtras(); 
		 	     String name = b.getString("name"); //This is our username...which we must pass continuously
		         b.putString("name", name);
		         //Add the set of extended data to the intent and start it
		         myIntent.putExtras(b);
		    	 startActivity(myIntent);
	    		
	    	}
	     });
	     
	     bTask.setOnClickListener(new View.OnClickListener() {
	        	/** 
	        	 * 
	        	 * Manipulates the text boxes upon a click of the available button. 
	        	 * 
	        	 * 
	        	 * @param v Default android param
	        	 *
	        	 */ 
	    		public void onClick(View v) 
	    		{
	    			  	 Intent myIntent = new Intent(v.getContext(), TaskView.class);   
	    		    	 Bundle b = getIntent().getExtras(); 
	    		 	     String name = b.getString("name"); //This is our username...which we must pass continuously
	    		         b.putString("name", name);
	    		         //Add the set of extended data to the intent and start it
	    		         myIntent.putExtras(b);
	    		    	 startActivity(myIntent);
	    			 
	    		}
	    	});
	     
	     
	     
	     bBack.setOnClickListener(new View.OnClickListener() {
	        	/** 
	        	 * 
	        	 * Manipulates the text boxes upon a click of the available button. 
	        	 * 
	        	 * 
	        	 * @param v Default android param
	        	 *
	        	 */ 
	    		public void onClick(View v) {
	    			 Intent myIntent = new Intent(v.getContext(), M5Activity.class);
                     startActivity(myIntent);	
	    		}
	    	});
	   
	findTasks(); 
	String[] entries = this.collected.split("\n");
	String[] csentries = new String[5];
	csentries[1] = "hj:g:k";
	int count = 0;
	for (int i=0; i < entries.length; i++)
	{
		if (entries[i].contains(name))
		{
			csentries[count] = entries[i];
			count++;
		}
		if (count == 4)
		{
			break;
		}
	}
	
	if (csentries[1] != "hj:g:k")
	{
		for (int j = 1; j < count; j++)
		{
			if (!csentries[j].contains("now complete"))
			{
				recenttasks.setText(csentries[j].split(":")[1] + " @ " + csentries[j].split(":")[2] + "\n" + "Due by: " + csentries[j].split(":")[3]);
			}
		}	
	}
	else
	{
		recenttasks.setText("No tasks pending!");
	}
	
	}
	
	
	

	
	

	public void findTasks()
	{
	
			//This routine is for reading the full contents of our key file until indicated
			FileInputStream fis = null;
			
			
			try 
			{
				fis = openFileInput("key");
				byte[] dataArray = new byte[fis.available()];
				while (fis.read(dataArray) != -1)
				{
					this.collected = new String(dataArray);
				}
			
			} 
			
			catch (FileNotFoundException e) 
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 	
			
			catch (IOException e) 
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			finally
			{
					try 
					{
						fis.close();
					} 
					catch (IOException e) 
					{
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
			}
			//"INDICATED"
			
		
		}
	}

